/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.amazon.node;

/**
 *
 * @author Salman Noor, Shcool of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email salmannoor74415@gmail.com
 * @License BSD License (http://www.linfo.org/bsdlicense.html)
 */
public class Operator extends Node {
    private Node LeftNode;
    private Node RightNode;
    

    public Operator(String operator,Node LeftNode, Node RightNode) throws InstantiationException {
        

        if (!operator.matches("^[+*]+$")){
            throw new InstantiationException("The operator "+operator+" is not valid.");
        }
        super.nodeValue = operator;
        this.LeftNode = LeftNode;
        this.RightNode = RightNode;
        
    }
    
    @Override
    public String getValue(){
        return super.nodeValue;
    }
    
    
    @Override
    public void setValue(String value){
        super.nodeValue = value;
    }

    @Override
    public Integer evaluate() {
        if (super.nodeValue.equals("*")){
            return this.LeftNode.evaluate() * this.RightNode.evaluate();    
        }else if (super.nodeValue.equals("+")){
            return this.LeftNode.evaluate() + this.RightNode.evaluate() ;
        }
        return null;
    }
    
    
    
}
